//
//  ReimbursementSQL.m
//  EASMobile
//
//  Created by jauleo on 15/12/2.
//  Copyright (c) 2015年 ABB. All rights reserved.
//

#import "ReimbursementSQL.h"

@implementation ReimbursementSQL
@synthesize db;

static ReimbursementSQL * sharedSingleton = nil;

+ (ReimbursementSQL *) sharedInstance
{
    if (sharedSingleton == nil) {
        sharedSingleton = [[ReimbursementSQL alloc] init];
        
    }
    return sharedSingleton;
}



- (id)init{
    if(self = [super init])
    {
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *sqlFilePath = [docPath stringByAppendingPathComponent:@"reimbursement.sqlite"];
        //原始sql文件路径
        NSString *originFilePath = [[NSBundle mainBundle] pathForResource:@"reimbursement" ofType:@"sqlite"];
        NSLog(@"1===%@ \n2=== %@",sqlFilePath,originFilePath);
        
        
        NSFileManager *fm = [NSFileManager defaultManager];//文件管理器
        if([fm fileExistsAtPath:sqlFilePath] == NO)//如果sql文件不在doc下，copy过来
        {
            NSError *error = nil;
            if([fm copyItemAtPath:originFilePath toPath:sqlFilePath error:&error] == NO)
            {
                NSLog(@"创建数据库的时候出现了错误：%@",[error localizedDescription]);
            }
        }
        
        self.db= [FMDatabase databaseWithPath:sqlFilePath] ;
        if (![db open]) {
            NSLog(@"Could not open db.");
        }
        [self.db setShouldCacheStatements:YES];
        
    }
    
    return self;
}

//表插入数据
- (BOOL)insertAppPlistSQL:(MEAPPManager *)appPlist;
{
    NSString * dbStr=[NSString stringWithFormat:@"INSERT INTO APPPlist (appId,appName,icon,version,plistUrl,schemesUrl,pkgname,appStatus,ipaUrl) VALUES ('%ld','%@','%@','%@','%@','%@','%@','%@','%@')",(long)appPlist.appId, appPlist.appName,appPlist.icon,appPlist.version,appPlist.plistUrl,appPlist.schemesUrl,appPlist.pkgname,appPlist.appStatus,appPlist.ipaUrl];
    
    [self.db executeUpdate:dbStr];
    if ([db hadError]) {
        NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
        return NO;
    }else {
        
    }
    
    return YES;
}

//查询表中数据

-(NSArray *)selectAppPlistSQLtable
{
     NSString * dbStr=[NSString stringWithFormat:@"SELECT * from %@",@"APPPlist"];
    FMResultSet * rs = [self.db executeQuery:dbStr];
    NSMutableArray * array = [NSMutableArray arrayWithCapacity:[rs columnCount]];
    while ([rs next]) {
        MEAPPManager *app = [[MEAPPManager alloc]init];
        app.appId = [rs intForColumn:@"appId"];
        app.icon = [rs stringForColumn:@"icon"];
        app.appName = [rs stringForColumn:@"appName"];
        app.ipaUrl = [rs stringForColumn:@"ipaUrl"];
        app.pkgname = [rs stringForColumn:@"pkgname"];
        app.schemesUrl = [rs stringForColumn:@"schemesUrl"];
        app.version = [rs stringForColumn:@"version"];
        app.plistUrl = [rs stringForColumn:@"plistUrl"];
        app.appStatus = [rs stringForColumn:@"appStatus"];
        

        [array addObject:app];
    }
    [rs close];
    return array;
}


//删除表中数据
-(void)deleteTable:(NSString *)table
{
    NSString * dbStr=[NSString stringWithFormat:@"delete from %@",table];
    
    [self.db executeUpdate:dbStr];
    if ([db hadError]) {
        NSLog(@"Error %d: %@", [db lastErrorCode], [db lastErrorMessage]);
    }
}






@end
